<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">{{this.$store.state.city}}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div class="button-wrapper"
               v-for="item of hot"
               v-bind:key="item.id"
               @click="handleCityClick(item.name)">
            <div class="button">{{item.name}}</div>
          </div>
        </div>
      </div>
      <!--右侧的字母表-->
      <div class="area" v-for="(item,key) of cities" v-bind:key="key" :ref="key">
        <div class="title border-topbottom">{{key}}</div> <!--城市地名 大写的英文字母  key值代表ABCD值-->
        <div class="item-list">
          <div class="item border-bottom" v-for="innerItem of item" v-bind:key="innerItem.id" @click="handleCityClick(innerItem.name)">
            {{innerItem.name}}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import BScroll from 'better-scroll'
  export default {
    name: "CityList",
    props: { // 这个属性的定义 用于子组件接收父组件传递的值
      hot: Array,
      cities: Object,
      //子组件监听到父组件City.vue传递的letter的数据内容了 并定义值的类型
      letter:String
    },
    //生命周期函数 当点击城市的时候 actions会被实时派发
    methods: {
      handleCityClick (city) {
        // this.$store.dispatch('changeCity',city)
        this.$store.commit('changeCity',city)
        this.$router.push('/')  //选中城市之后就跳转到首页
      }
    },
    watch: {  //vue自带的监听器 用来监听当letter的数据发生改变时 list列表也同步进行改变
      letter () {
        if (this.letter) {//如果letter不为空的话 调用better-scroll 滚动的方法 自动滚动到某个区域的元素上
          const element = this.$refs[this.letter][0]
          this.scroll.scrollToElement(element)
        }
        // console.log(this.letter)
      }
    },
    //生命周期函数  它会在页面挂载完毕后执行引入的第三方滚动插件 better-scroll
    mounted () {
      // this.scroll = new BScroll(this.$refs.wrapper)
    },
  }
</script>

<style lang="stylus" scoped>
  @import '~styles/varibles.styl'
  .border-topbottom
    &:before
      border-color: #ccc
    &:after
      border-color: #ccc
  .border-bottom
    &:before
      border-color: #ccc
  .list
    overflow: hidden
    position: absolute
    top: 1.58rem
    left: 0
    right: 0
    bottom: 0
    .title
      line-height: .54rem
      background: #eee
      padding-left: .2rem
      color: #666
      font-size: .26rem
    .button-list
      overflow: hidden
      padding: .1rem .6rem .1rem .1rem
      .button-wrapper
        float: left
        width: 33.33%
        .button
          margin: .1rem
          padding: .1rem 0
          text-align: center
          border: .02rem solid #ccc
          border-radius: .06rem
    .item-list
      .item
        line-height: .76rem
        padding-left: .2rem
        font-size .22rem
</style>
